/*
 * Copyright 2020-8-27 Baheal
 * @(#)OAProducer.java
 */
package com.byyy.ccts.core.message.rocketmq;

import com.alibaba.fastjson.JSON;
import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.message.rocketmq.config.Configuration;
import com.byyy.ccts.core.utils.byyyoa.OAModel;
import com.byyy.ccts.core.utils.byyyoa.OAUtils;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.nio.charset.StandardCharsets;

/**
 * @author liyongbo
 * @version 1.0
 * @date 2020-8-27 15:51
 * @description： 生产者
 */
@Component(value = "oAProducer")
public class OAProducer implements ILogBase {

    private DefaultMQProducer defaultMQProducer;

    @Autowired
    private Configuration config;

    /**
     * 初始化
     * @throws MQClientException
     */
    @PostConstruct
    public void init() throws MQClientException {
        this.defaultMQProducer = new DefaultMQProducer(config.getProducerGroup());
        defaultMQProducer.setNamesrvAddr(config.getNamesrvAddr());
        defaultMQProducer.setInstanceName(config.getInstanceName());
        //关闭VIP通道，避免出现connect to <:10909> failed导致消息发送失败
        defaultMQProducer.setVipChannelEnabled(false);
        defaultMQProducer.start();
        logger.info("RocketMq 生产者启动完毕");
    }

    /**
     * 发送消息
     * @param topic
     * @param tag
     * @param content
     */
    public void send(String topic, String tag, String content) {
        Message msg = new Message(topic, tag, content.getBytes(StandardCharsets.UTF_8));
        try {
            this.defaultMQProducer.send(msg);
            logger.debug("消息发送完毕[{}-{}-{}]", topic, tag, content);
        } catch (Exception e) {
            OAUtils.sendMail("exception", "消息发送失败", e.getMessage(), "N");
            logger.error("发送消息失败!", e);
        }
    }

    /**
     * 发送OA邮件
     * @param content
     */
    public void sendOAMail(String code, String title, String content) {
        OAModel oaModel = new OAModel(code, title, content);
        this.send(config.getTopic(), Configuration.TAG_MESSAGE, JSON.toJSONString(oaModel));
    }

    /**
     * 发送OA邮件连带短信
     * @param content
     */
    public void sendOAMailWithMsg(String code, String title, String content, String ext) {
        OAModel oaModel = new OAModel(code, title, content, ext);
        this.send(config.getTopic(), Configuration.TAG_MESSAGE, JSON.toJSONString(oaModel));
    }

    /**
     * 发送回传WMS冷链装车消息
     * @param content
     */
    public void sendBackWmsMsg(String content) {
        this.send(config.getTopic(), Configuration.TAG_BACK_WMS_ZCD, content);
    }

    @PreDestroy
    public void destroy() {
        defaultMQProducer.shutdown();
    }

    public DefaultMQProducer getDefaultMQProducer() {
        return defaultMQProducer;
    }
}
